package com.bw.activity7.activiti.controller;

import com.bw.activity7.activiti.pojo.ActivitiVo;
import com.bw.activity7.activiti.pojo.UserTask;
import com.bw.activity7.activiti.service.ActivitiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activiti/")
public class ActivitiController {

    @Autowired
    private ActivitiService activitiService;



    /**
     * 请假流程发起
     *
     */
    @PostMapping("askForBiz")
    public void askForBiz(ActivitiVo activitiVo){
        activitiService.askForBiz(activitiVo);
    }

    /**
     * 查看用户当前任务
     */
    @GetMapping("queryUserTaskByUserName")
    public Map<String,Object> queryUserTaskByUserName(ActivitiVo activitiVo) {
        Map<String, Object> resultMap = new HashMap<>();
        List<UserTask> list = activitiService.queryUserTaskByUserName(activitiVo);;
        resultMap.put("taskList", list);
        resultMap.put("username",activitiVo.getUsername());
        return resultMap;
    }

    /**
     * 查询用户发起的流程
     */
    @GetMapping("getHistoricProcessInstanceByUserName")
    public Map<String,Object> getHistoricProcessInstance(ActivitiVo activitiVo) {
        Map<String, Object> resultMap = new HashMap<>();
        ArrayList<Map> list = activitiService.getHistoricProcessInstance(activitiVo);
        resultMap.put("taskList", list);
        resultMap.put("username",activitiVo.getUsername());
        return resultMap;
    }

    /**
     * 完成一个任务
     */
    @PostMapping("completeUserTaskById")
    public void completeUserTaskById(@RequestBody ActivitiVo activitiVo) {
        activitiService.completeUserTaskById(activitiVo);
    }

    /**
     * 根据流程实例id查看当前流程图
     */
    @GetMapping("getFlowImgByInstanceId")
    public void getFlowImgByInstanceId(String processInstanceId, HttpServletResponse response) throws IOException  {
        //设置页面不缓存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        ServletOutputStream outputStream = response.getOutputStream();

        //设置输出的内容的类型为JPEG图像
        response.setContentType("image/svg");

        //写给浏览器
        activitiService.getFlowImgByInstanceId(processInstanceId,outputStream);

        outputStream.flush();
    }
}
